import numpy as np
import csv
import random
from datetime import datetime
import math as math
import bisect


def getCurTime():
    """
    get current time
    Return value of the date string format(%Y-%m-%d %H:%M:%S)
    """
    format='%Y-%m-%d %H:%M:%S'
    sdate = None
    cdate = datetime.now()
    try:
        sdate = cdate.strftime(format)
    except:
        raise ValueError
    return sdate

def build_list(inputCSV):
    sKey = np.array([])
    fn = inputCSV
    ra = csv.DictReader(file(fn), dialect="excel")
    for record in ra:
        #print record[ra.fieldnames[0]], type(record[ra.fieldnames[-1]])
        for item in ra.fieldnames:
            temp = float(record[item])
            sKey =np.append(sKey, temp)
    sKey.shape=(-1,len(ra.fieldnames))
    return sKey

def build_region_list(inputCSV):
    fn = inputCSV
    ra = csv.DictReader(file(fn), dialect="excel")
    temp_list = []
    for record in ra:
        temp_list.append(int(float(record[ra.fieldnames[-1]])))
    return temp_list
        

#--------------------------------------------------------------------------
#MAIN

if __name__ == "__main__":
    print "begin at " + getCurTime()

    #filePath = "C:/_DATA/CancerData/"
    top_ten_lambda_csv = "C:/_DATA/CancerData/test/Jan15/Thousand/10000/cancer179/new_contiguity/0420/WARD_SO_10000_cancer179_top_ten_lambda.csv"
    top_ten_lambda = build_list(top_ten_lambda_csv)
    region_lambda_csv = "C:/_DATA/CancerData/test/Jan15/Thousand/10000/cancer179/new_contiguity/0420/WARD_SO_10000_cancer179_region_lambda.csv"
    #print top_ten_lambda.shape
    region_lambda = build_list(region_lambda_csv)
    #print region_lambda

    top = 2
    temp_lambda = np.array([])
    #p_value = np.zeros(len(region_lambda))
    p_value = np.array([])
    p_value = np.append(p_value, region_lambda[:,0])
    #print p_value
    for top in range(1,11):
        for i in range(top):
            temp_lambda = np.append(temp_lambda, top_ten_lambda[:,i+1])
        for item in region_lambda[:,1]:
            temp = np.where(temp_lambda > item)
            p_value = np.append(p_value, (len(temp[0])+0.0)/(len(temp_lambda)+1))
    #np.savetxt(pvalue_path, pvalue, delimiter=',')
    p_value.shape = (-1, len(region_lambda))
    p_value = np.transpose(p_value)
    
    filepath = region_lambda_csv[:-17] + "p_value_10.csv"
    #print filepath
    np.savetxt(filepath, p_value, delimiter=',')
    print "end at " + getCurTime()
    print "========================================================================"  
           
